/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved.
 */
package com.icinfo.cloud.provider.punish.ucase.casemain.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.icinfo.cloud.common.security.util.UserUtil;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.exception.BusinessException;
import com.icinfo.cloud.provider.punish.ucase.casemain.dto.CaseAttachmentInfoQueryDto;
import com.icinfo.cloud.provider.punish.ucase.casemain.dto.CaseOfflineDocumentRecordSaveDto;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.CaseOfflineDocumentRecordMapper;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseOfflineDocumentQueryDto;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseOfflineDocumentRecord;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseAppendixRecordService;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseMainInfoService;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseOfflineDocumentRecordService;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.*;
import com.icinfo.cloud.xzcf.provider.member.userinfo.dto.UserDTO;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import com.icinfo.framework.tools.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 描述: 案件线下文书记录表 case_offline_document_record 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年07月26日
 */
@Service
public class CaseOfflineDocumentRecordServiceImpl extends MyBatisServiceSupport implements ICaseOfflineDocumentRecordService {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(CaseOfflineDocumentRecordServiceImpl.class);

    @Resource
    private CaseOfflineDocumentRecordMapper caseOfflineDocumentRecordMapper;
    @Resource
    private ICaseMainInfoService caseMainInfoService;
    @Resource
    private ICaseAppendixRecordService caseAppendixRecordService;

    @Override
    public int saveCaseOfflineDocument(CaseOfflineDocumentRecordSaveDto saveDto) {
        UserDTO userInfo = UserUtil.getUserInfo();
        if (ObjectUtil.isEmpty(userInfo)) {
            throw new BusinessException("未登录或登录信息已失效");
        }
        CaseMainInfoVo caseMainInfo = caseMainInfoService.getCaseMainInfo(saveDto.getCaseId());
        CaseOfflineDocumentRecord offlineDocumentRecord = new CaseOfflineDocumentRecord();
        BeanUtils.copyProperties(saveDto, offlineDocumentRecord);
        offlineDocumentRecord.setCaseLinkCode(caseMainInfo.getCaseCurrentLinkCode());
        offlineDocumentRecord.setCaseLinkName(caseMainInfo.getCaseCurrentLinkName());
        offlineDocumentRecord.setCaseStageCode(caseMainInfo.getCaseCurrentStageCode());
        offlineDocumentRecord.setCaseStageName(caseMainInfo.getCaseCurrentStageName());
        offlineDocumentRecord.setCreateUserId(userInfo.getUserId().toString());
        offlineDocumentRecord.setCreateUserName(userInfo.getRealName());
        offlineDocumentRecord.setStatus(Constant.STRING_1);
        if (ObjectUtil.isNotEmpty(saveDto.getDocumentId())) {
            offlineDocumentRecord.setId(saveDto.getDocumentId());
            return caseOfflineDocumentRecordMapper.updateByPrimaryKeySelective(offlineDocumentRecord);
        } else {
            offlineDocumentRecord.setId(StringUtils.uuid());
            offlineDocumentRecord.setCreateTime(LocalDateTime.now());
            return caseOfflineDocumentRecordMapper.insertSelective(offlineDocumentRecord);
        }
    }

    @Override
    public CaseOfflineDocumentDetailVo getCaseOfflineDocumentDetail(CaseOfflineDocumentQueryDto queryDto) {
        Example example = new Example(CaseOfflineDocumentRecord.class);
        example.createCriteria().andEqualTo("id", queryDto.getDocumentId())
                .andEqualTo("caseId", queryDto.getCaseId());
        List<CaseOfflineDocumentRecord> caseOfflineDocumentRecords = caseOfflineDocumentRecordMapper.selectByExample(example);
        if (caseOfflineDocumentRecords == null || caseOfflineDocumentRecords.size() == 0) {
            throw new BusinessException("查询的线下文书不存在");
        }
        CaseOfflineDocumentDetailVo detailVo = new CaseOfflineDocumentDetailVo();
        BeanUtils.copyProperties(caseOfflineDocumentRecords.get(0), detailVo);
        // 获取其他附件
        //查询保存的附件
        String documentCatalogCode = detailVo.getDocumentCatalogCode();
        if (ObjectUtil.isNotEmpty(documentCatalogCode)) {
            CaseAttachmentInfoQueryDto queryAttachment = new CaseAttachmentInfoQueryDto();
            queryAttachment.setCaseId(queryDto.getCaseId());
            queryAttachment.setDocumentCatalogCode(detailVo.getDocumentCatalogCode());
            List<CaseAttachmentInfoVo> caseAttachmentList = caseAppendixRecordService.getCaseAttachmentInfoList(queryAttachment);
            detailVo.setAppendixList(caseAttachmentList);
        }
        return detailVo;
    }

    @Override
    public List<CaseStageLinkRelateDocumentVo> getOfflineDocInfoToCaseStageLinkRelateDocument(String caseId) {
        List<CaseStageLinkRelateDocumentVo> caseStageLinkRelateDocumentVos = caseOfflineDocumentRecordMapper.selectOfflineDocInfoToCaseStageLinkRelateDocument(caseId);
        return caseStageLinkRelateDocumentVos;
    }

    @Override
    public int deleteCaseOfflineDocument(CaseOfflineDocumentQueryDto queryDto) {
        Example example = new Example(CaseOfflineDocumentRecord.class);
        example.createCriteria().andEqualTo("id", queryDto.getDocumentId())
                .andEqualTo("caseId", queryDto.getCaseId());
        return caseOfflineDocumentRecordMapper.deleteByExample(example);
    }

    /**
     * 描述：根据id获取所有的线下文书信息
     *
     * @param caseId 用例id
     * @author tianrunjia
     * @date 2022-08-28
     */
    @Override
    public List<CaseDocumentCatalogChainOfflineDetailVo> doGetCaseDocumentCatalogChainOfflineDetailVo(String caseId) {
        return caseOfflineDocumentRecordMapper.selectCaseDocumentCatalogChainOfflineDetailVo(caseId);
    }
    /**
     * 【V1.3.0】描述：根据环节编码撤回线下上传文书
     *
     * @param caseId       用例id
     * @param caseLinkCode 情况下链接代码
     * @author shishengyao
     * @date 2022/11/16
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void recallCaseOfflineDocumentByCaseLinkCode(String caseId, String caseLinkCode) {
          caseOfflineDocumentRecordMapper.updateRecallCaseOfflineDocumentByCaseLinkCode(caseId,caseLinkCode);
    }

    /**
     * 获取案件某个阶段的所有线下文书
     * @param caseId
     * @param caseStageCode
     * @return
     */
    @Override
    public List<CaseOfflineDocumentRecord> getCaseOfflineDocumentRecord(String caseId, String caseStageCode){
        Example example = new Example(CaseOfflineDocumentRecord.class);
        example.createCriteria().andEqualTo("caseStageCode", caseStageCode)
                .andEqualTo("caseId", caseId)
                .andEqualTo("status", "1");
        return caseOfflineDocumentRecordMapper.selectByExample(example);
    }

    @Override
    public List<CaseOfflineDocumentRecord> getCaseOfflineDocumentRecordByKindCodes(String caseId, List<String> documentKindCodes) {
        Example example = new Example(CaseOfflineDocumentRecord.class);
        example.createCriteria().andEqualTo("caseId", caseId)
                .andEqualTo("status", "1").andIn("documentKindCode", documentKindCodes);
        return caseOfflineDocumentRecordMapper.selectByExample(example);
    }
}
